package cn.echisan.homework.controller;

import cn.echisan.homework.annotation.HasAnyRoles;
import cn.echisan.homework.enums.Role;
import cn.echisan.homework.model.Score;
import cn.echisan.homework.model.User;
import cn.echisan.homework.service.ScoreService;
import cn.echisan.homework.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by echisan on 2018/5/29
 */
@Controller
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private UserService userService;

    @Autowired
    private ScoreService scoreService;

    @GetMapping
    public String renderHome(HttpSession session, Model model,
                             @RequestParam(value = "msg",required = false, defaultValue = "")String msg) {
        if (!msg.equals("")){
            model.addAttribute("msg", msg);
        }
        User sessionUser = (User) session.getAttribute("user");
        if (sessionUser.getRole().equals(Role.ROLE_STUDENT.name())) {
            User user = userService.getUserById(sessionUser.getId());
            model.addAttribute("user", user);
            List<Score> scores = scoreService.listUserScoreByUserId(user.getId());
            model.addAttribute("scores", scores);
            return "home";
        } else if (sessionUser.getRole().equals(Role.ROLE_TEACHER.name())) {
            User user = userService.getUserById(sessionUser.getId());
            model.addAttribute("user", user);

            List<User> students = userService.listStudents();
            model.addAttribute("students", students);
            return "home";
        }
        User user = userService.getUserById(sessionUser.getId());
        model.addAttribute("user", user);
        List<User> teachersList = userService.listTeachers();
        List<User> studentsList = userService.listStudents();
        model.addAttribute("teachersList", teachersList);
        model.addAttribute("studentsList", studentsList);
        return "home";
    }

    @GetMapping("/student/{id}")
    public String getUserCourseInfo(@PathVariable(value = "id") Integer id, Model model) {
        List<Score> scores = scoreService.listUserScoreByUserId(id);
        model.addAttribute("scores", scores);
        return "stu_course_info";
    }

    @HasAnyRoles(Role.ROLE_ADMIN)
    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") Integer id, Model model) {
        userService.deleteUserById(id);
        model.addAttribute("msg", "删除成功");
        return "home";
    }

    @PostMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id") Integer id, @ModelAttribute("user") User user, Model model) {
        user.setId(id);

        try {
            userService.updateUser(user);
        } catch (Exception e){
            model.addAttribute("error","修改失败,原因"+e.getMessage()+"");
        }
        model.addAttribute("msg","修改成功");
        return "redirect:/home";
    }
}
